package mpc;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

public class TableDrawer extends JPanel implements MouseListener{
	
	private VirtualField vfield;
	private IMode mode;
	
	final public static int  //MAZE_WIDTH = VirtualField.MAZE_WIDTH, MAZE_HEIGHT = VirtualField.MAZE_HEIGHT,
			SQ_SIZE = 50, DIST = 2 ;//MODE_X = VirtualField.MAZE_WIDTH * (SQ_SIZE + DIST + 1), MODE_W = 150, MODE_H = 100;
			//dX = -8, dY = -35;
			//dx, dy;
			
	private int dX = -8, dY = -35;
	
	public TableDrawer() {
		// TODO Auto-generated constructor stub
		//vfield = new VirtualField(MAZE_WIDTH, MAZE_HEIGHT);
		vfield = GUIClass.VFIELD;
		//this.setBounds(0, 0, MAZE_WIDTH*(SQ_SIZE + DIST), MAZE_HEIGHT*(SQ_SIZE + DIST));
	}
	
	public void configureMouse(){
		Point p = this.getLocation();
		//Rectangle p = this.getBounds();
		dX = -p.x; dY = -p.y;
		System.out.println("left-top corner: x: " + p.x + " y: " + p.y + "\nwidth :");
	}
	
	private XYPair findBox(int x, int y){
		x += dX; y += dY;
		//this.addMouseListener(this);
		if(!((x >= 0) && (x < VirtualField.MAZE_WIDTH * (SQ_SIZE)) && (y >= 0) && (y < VirtualField.MAZE_HEIGHT * (SQ_SIZE)))) return null;
		return new XYPair(x / (SQ_SIZE), y / (SQ_SIZE));
		
	}
	
	private Color chooseColorForState(int state){
		if(state == VirtualField.INF) return Color.yellow;
		if(state == VirtualField.START) return Color.green;
		if(state == VirtualField.BRICK) return Color.darkGray;
		if(state == VirtualField.IN_QUEUE) return Color.magenta;
		if(state == VirtualField.FIRST_IN_QUEUE) return Color.blue;
		if(state == VirtualField.WORKED) return Color.cyan;
		//if(state == VirtualField.IN_PATH_TO_FINISH) return Color.pink;
		
		return null;
	}
	
	@Override 
	public void paintComponent(Graphics g) {
		//this.setPreferredSize(new Dimension(VirtualField.MAZE_WIDTH *(SQ_SIZE + DIST), VirtualField.MAZE_HEIGHT * (SQ_SIZE + DIST)));
		g.setColor(Color.black);
		g.fillRect(0, 0, VirtualField.MAZE_WIDTH * (SQ_SIZE), VirtualField.MAZE_HEIGHT * (SQ_SIZE));
		//g.setColor(Color.yellow);
		for(int i = 0; i < VirtualField.MAZE_WIDTH; i++){
			for(int j = 0; j < VirtualField.MAZE_HEIGHT; j++){
				/*if(vfield.getState(i, j) == VirtualField.INF) g.setColor(Color.yellow);
				else if(vfield.getState(i, j) == 0) g.setColor(Color.green);
				else if(vfield.getState(i, j) == VirtualField.BRICK) g.setColor(Color.DARK_GRAY);
				else 
					*/
				
				
				g.setColor(chooseColorForState(vfield.getState(i, j)));
				
				if(vfield.isInPath(i, j))g.setColor(Color.pink);
				
				g.fillRect(i * (SQ_SIZE), j * (SQ_SIZE), SQ_SIZE - DIST, SQ_SIZE - DIST);
				
				g.setColor(Color.black);
				g.drawString(""+vfield.getDist(i, j), i * (SQ_SIZE) + 10, j * (SQ_SIZE) + 20);
				
				//g.setFont(Font.)
				if(vfield.numberInQueue(i, j) != -1){
					g.drawString("" + vfield.numberInQueue(i, j), i * (SQ_SIZE) + 20, j * (SQ_SIZE) + 40);
				}
				
				g.setColor(Color.DARK_GRAY);
				g.fillOval(i * (SQ_SIZE) - 7, j * (SQ_SIZE) - 7, 12, 12);
			}
		}
		
    }

	public VirtualField getField(){
		return vfield;
	}
	
	public void setMode(IMode mode){
		this.mode = mode;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		//e.setSource(this);
		System.out.println("Click Location: " + (e.getX() + dX) + " " + (e.getY() + dY));
		XYPair b = findBox(e.getX(), e.getY());
		if(b != null){
			mode.clickOn(b.x, b.y);
			this.repaint();
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
